package com.datamation.swadeshi.control.data;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.datamation.swadeshi.model.FinvDetL3;

public class FinvDetL3DS {
	private SQLiteDatabase dB;
	private DatabaseHelper dbHelper;
	Context context;
	private String TAG="swadeshi";
	
	public FinvDetL3DS (Context context){
		this.context = context;
		dbHelper = new DatabaseHelper(context);
	}
	
	public void open() throws SQLException {
		dB = dbHelper.getWritableDatabase();
	}
	
	@SuppressWarnings("static-access")
	public int createOrUpdateFinvDetL3(ArrayList<FinvDetL3>  list) {
		int count =0;		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		
	try{
			for (FinvDetL3 finvDetL3 : list) {
				
				ContentValues values = new ContentValues();
			
				values.put(dbHelper.FINVDETL3_AMT, finvDetL3.getFINVDETL3_AMT());
				values.put(dbHelper.FINVDETL3_ITEM_CODE, finvDetL3.getFINVDETL3_ITEM_CODE());
				values.put(dbHelper.FINVDETL3_QTY, finvDetL3.getFINVDETL3_QTY());
				values.put(dbHelper.FINVDETL3_REF_NO, finvDetL3.getFINVDETL3_REF_NO());
				values.put(dbHelper.FINVDETL3_SEQ_NO, finvDetL3.getFINVDETL3_SEQ_NO());
				values.put(dbHelper.FINVDETL3_TAX_AMT, finvDetL3.getFINVDETL3_TAX_AMT());
				values.put(dbHelper.FINVDETL3_TAX_COM_CODE, finvDetL3.getFINVDETL3_TAX_COM_CODE());
				values.put(dbHelper.FINVDETL3_TXN_DATE, finvDetL3.getFINVDETL3_TXN_DATE());
			
				
				count = (int) dB.insert(dbHelper.TABLE_FINVDETL3, null, values);
		
			}
			}finally {  
				if (cursor !=null) {
					cursor.close();
				}
				dB.close();
			}
			return count;
	}
	
	@SuppressWarnings("static-access")
	public int deleteAll(){
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		try{
			cursor = dB.rawQuery("SELECT * FROM " + dbHelper.TABLE_FINVDETL3, null);
			count =cursor.getCount();
			if(count>0){
				int success = dB.delete(dbHelper.TABLE_FINVDETL3, null, null);
				Log.v("Success", success+"");
			}
		}catch (Exception e){

			Log.v(TAG+" Exception", e.toString());
			
		}finally{  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		
		return count;
		
	}
}
